/*
	MIDAS Context Based Routing
	cbr_triple.cc
	Copyright (c) 2007 Radoslaw Olgierd Schoeneich
	MEAG IT PW
*/

#include "cbr_triple.h"
#include "cbr_context_model.h"
#include "cbr_crt.h"

extern bool local_context_model;

/**
 * CBR triple constructor.
 *
 * @param t1 first CBR triple
 * @param t2 second CBR triple
 * @param model CBR context model
 *
 * Constructs new CBR triple based on two existing CBR triples.
 *
 * Concept of the new CBR triple is set to the closest atomic concept
 * for the two concept of existing CBR triples.  The source node CBR id
 * and hop count are then set to be of the CBR triple with the smaller
 * hop count.
 *
 * @return new CBR triple.
 */

cbr_Triple::cbr_Triple(const cbr_Triple& t1, const cbr_Triple& t2,
						const cbr_ContextModel* model)
{
	if (local_context_model)
		this->concept = model->findClosestAtomicConcept(t1.getConcept(),
								t2.getConcept());
	else
		this->concept = cbr_getClosestCommonSuperConcept(t1.getConcept(),
								 t2.getConcept());

	if (t1.hop_cnt <= t2.hop_cnt) {
		this->hop_cnt = t1.hop_cnt;
		this->src = t1.src;
	} else {
		this->hop_cnt = t2.hop_cnt;
		this->src = t2.src;
	}
}

/**
 * Get byte length of the CBR triple.
 *
 * CBR triple length == concept length + 1 + 1 + 4
 *
 * @todo why this is + 1 + 1 + 4?
 *
 * @return byte length of the CBR triple.
 */

unsigned cbr_Triple::getByteLength() const
{
	return concept.length() + 1 + 1 + 4;
}

/**
 * Dump CBR triple to the debug log.
 *
 * @param triple CBR triple
 */

void cbr_Triple_dump(const cbr_Triple *triple)
{
	cbr_debug(" triple: '%s' @ node %u is %u hop(s) away\n",
		  triple->getConcept().c_str(), triple->getSource(),
		  (unsigned)triple->getMetric());
}

/**
 * Dump CBR triples list to the debug log.
 *
 * @param s name of the list
 * @param list CBR triples list
 */

void cbr_TriplesList_dump(char *s, const cbr_TriplesList *list)
{
	cbr_TriplesList::const_iterator i;

	cbr_debug("%s list:\n", s);

	for (i = list->begin(); i != list->end(); i++) {
		cbr_Triple *triple = (*i);
		cbr_Triple_dump(triple);
	}
}
